package cn.ctodb.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.cms.service.CmsFileService;
import cn.ctodb.cms.web.rest.util.HeaderUtil;
import cn.ctodb.cms.service.dto.CmsFileDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CmsFile.
 */
@RestController
@RequestMapping("/api")
public class CmsFileResource {

    private final Logger log = LoggerFactory.getLogger(CmsFileResource.class);
        
    @Inject
    private CmsFileService cmsFileService;

    /**
     * POST  /cms-files : Create a new cmsFile.
     *
     * @param cmsFileDTO the cmsFileDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cmsFileDTO, or with status 400 (Bad Request) if the cmsFile has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cms-files")
    @Timed
    public ResponseEntity<CmsFileDTO> createCmsFile(@RequestBody CmsFileDTO cmsFileDTO) throws URISyntaxException {
        log.debug("REST request to save CmsFile : {}", cmsFileDTO);
        if (cmsFileDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("cmsFile", "idexists", "A new cmsFile cannot already have an ID")).body(null);
        }
        CmsFileDTO result = cmsFileService.save(cmsFileDTO);
        return ResponseEntity.created(new URI("/api/cms-files/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("cmsFile", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cms-files : Updates an existing cmsFile.
     *
     * @param cmsFileDTO the cmsFileDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cmsFileDTO,
     * or with status 400 (Bad Request) if the cmsFileDTO is not valid,
     * or with status 500 (Internal Server Error) if the cmsFileDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cms-files")
    @Timed
    public ResponseEntity<CmsFileDTO> updateCmsFile(@RequestBody CmsFileDTO cmsFileDTO) throws URISyntaxException {
        log.debug("REST request to update CmsFile : {}", cmsFileDTO);
        if (cmsFileDTO.getId() == null) {
            return createCmsFile(cmsFileDTO);
        }
        CmsFileDTO result = cmsFileService.save(cmsFileDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("cmsFile", cmsFileDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cms-files : get all the cmsFiles.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cmsFiles in body
     */
    @GetMapping("/cms-files")
    @Timed
    public List<CmsFileDTO> getAllCmsFiles() {
        log.debug("REST request to get all CmsFiles");
        return cmsFileService.findAll();
    }

    /**
     * GET  /cms-files/:id : get the "id" cmsFile.
     *
     * @param id the id of the cmsFileDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cmsFileDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cms-files/{id}")
    @Timed
    public ResponseEntity<CmsFileDTO> getCmsFile(@PathVariable Long id) {
        log.debug("REST request to get CmsFile : {}", id);
        CmsFileDTO cmsFileDTO = cmsFileService.findOne(id);
        return Optional.ofNullable(cmsFileDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /cms-files/:id : delete the "id" cmsFile.
     *
     * @param id the id of the cmsFileDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cms-files/{id}")
    @Timed
    public ResponseEntity<Void> deleteCmsFile(@PathVariable Long id) {
        log.debug("REST request to delete CmsFile : {}", id);
        cmsFileService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("cmsFile", id.toString())).build();
    }

    /**
     * SEARCH  /_search/cms-files?query=:query : search for the cmsFile corresponding
     * to the query.
     *
     * @param query the query of the cmsFile search 
     * @return the result of the search
     */
    @GetMapping("/_search/cms-files")
    @Timed
    public List<CmsFileDTO> searchCmsFiles(@RequestParam String query) {
        log.debug("REST request to search CmsFiles for query {}", query);
        return cmsFileService.search(query);
    }


}
